import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 东莞呵呵
 * Date:2022-08-26
 * Time:21:08
 */
public class JDBCUpdate {
    public static void main(String[] args) throws SQLException {
        //1、创建数据源
        DataSource dataSource=new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("123456");

        //2、建立连接
        Connection connection=dataSource.getConnection();

        //输入操作
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入需要被修改信息的id");
        int id=scanner.nextInt();
        System.out.println("请输入需要修改后的名字");
        String name=scanner.next();

        //3、构造sql语句
        String sql="update student set name = ? where id = ?";
        PreparedStatement statement= connection.prepareStatement(sql);
        statement.setInt(2,id);
        statement.setString(1,name);

        //4、执行sql语句
        int n = statement.executeUpdate();
        System.out.println("n = " + n);

        //5、关闭资源
        statement.close();
        connection.close();
    }
}
